<?php
/*
	Aurus CMS - free OOP CMS
	Copyright (C) 2010  Nestor Yanchuk

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
if (! defined('ROOT')) die();

class Module_faq implements Module{
    public function run(){
        $qus = Mysql::getInstance()->select('faq');

        for ($i = 0,$n = count($qus);$i < $n;$i++) {
            $q = $qus[$i];

            $tpl = Template::getInstance();
            $tpl->load('faq_row');
            $tpl->set('{id}',$i);
            $tpl->set('{answer}',stripslashes($q['answer']));
            $tpl->set('{question}',stripslashes($q['question']));

            Aurus::getInstance()->addContent($tpl->out());
        }

        if (!count($qus)) {
            Aurus::getInstance()->setError(__('Питання відсутні, зайдіть пізніше.'));
        }

        Aurus::getInstance()->setTitle(__('Часті питання'));
    }

    public function onActivate(){
        $s = "CREATE TABLE IF NOT EXISTS `".PREFIX."_faq` (`qid` INT NOT NULL AUTO_INCREMENT ,`question` TEXT NOT NULL ,`answer` TEXT NOT NULL ,PRIMARY KEY ( `qid` )) ENGINE = MYISAM ;";
        Mysql::getInstance()->query($s);
    }
    public function onDeactivate(){
        Mysql::getInstance()->query("DROP TABLE `".PREFIX."_faq`");
    }

    public function initAdminArea(){
        if (isset($_GET['act']) && $_GET['act'] == 'add') {
            if (!empty($_POST['qst']) && !empty($_POST['ans'])) {
                $q = Mysql::getInstance()->escape(Parser::getInstance()->parse($_POST['qst']));
                $a = Mysql::getInstance()->escape(Parser::getInstance()->parse($_POST['ans']));
                Mysql::getInstance()->query("INSERT INTO `" . PREFIX . "_faq` (question,answer) VALUES ('{$q}','{$a}')");
                Aurus::getInstance()->addContent(Adminarea::getInstance()->adminInfo(__('Запитання додано')));
            } else {
                Aurus::getInstance()->addContent(Aurus::getInstance()->form_start() . Html::table(Html::tr(Html::td('', '', 'left', '15%') . Html::td(Aurus::getInstance()->getEditor(), '', 'left', '85%')) . Html::tr(Html::td(__('Запитання'), '', 'left', '15%') . Html::td('<textarea style="width:100%;height:300px;" id="qst" name="qst"></textarea>', '', 'left', '85%')) . Html::tr(Html::td(__('Відповідь'), '', 'left', '15%') . Html::td('<textarea style="width:100%;height:300px;" id="ans" name="ans"></textarea>', '', 'left', '85%'))) . '<br /><center><input class="input" type="submit" value="' . __('Зберегти') . '"></center></form>');
            }
        } elseif (isset($_GET['act']) && $_GET['act'] == 'del') {
            Mysql::getInstance()->query("DELETE FROM `" . PREFIX . "_faq` WHERE qid='{$_GET['did']}'");
            Aurus::getInstance()->addContent(Adminarea::getInstance()->adminInfo('Питання видалено'));
        } elseif (isset($_GET['act']) && $_GET['act'] == 'edit') {
            if (!empty($_POST['qst']) && !empty($_POST['ans'])) {
                $q = Mysql::getInstance()->escape(Parser::getInstance()->parse($_POST['qst']));
                $a = Mysql::getInstance()->escape(Parser::getInstance()->parse($_POST['ans']));
                Mysql::getInstance()->query("UPDATE `" . PREFIX . "_faq` SET question='$q',answer='$a' WHERE qid='{$_GET['did']}'");
                Aurus::getInstance()->addContent( Adminarea::getInstance()->adminInfo(__('Зміни збережено')) );
            } else {
                $qa = Mysql::getInstance()->getarray("SELECT * FROM `" . PREFIX . "_faq` WHERE qid='{$_GET['did']}'");

                $quest = Parser::getInstance()->unParseBB(stripslashes($qa['question']));
                $ans = Parser::getInstance()->unParseBB(stripslashes($qa['answer']));

                Aurus::getInstance()->addContent( Aurus::getInstance()->form_start() . Html::table(Html::tr(Html::td('', '', 'left', '15%') . Html::td(Aurus::getInstance()->getEditor(), '', 'left', '85%')) . Html::tr(Html::td(__('Запитання'), '', 'left', '15%') . Html::td('<textarea style="width:100%;height:300px;" id="qst" name="qst">' . $quest . '</textarea>', '', 'left', '85%')) . Html::tr(Html::td(__('Відповідь'), '', 'left', '15%') . Html::td('<textarea style="width:100%;height:300px;" id="ans" name="ans">' . $ans . '</textarea>', '', 'left', '85%'))) . '<br /><center><input class="input" type="submit" value="' . __('Зберегти') . '"></center></form>');
            }
        } else {
            //List of questions
            $q = Mysql::getInstance()->query("SELECT * FROM `" . PREFIX . "_faq`");
            $cont = '';
            while ($qu = Mysql::getInstance()->fetch_assoc($q)) {
                $quest = strip_tags(stripslashes($qu['question']));
                if (strlen($quest) > 80) {
                    $quest = substr($quest, 0, 80) . '...';
                }
                $answ = strip_tags(stripslashes($qu['answer']));
                if (strlen($answ) > 80) {
                    $quest = substr($answ, 0, 80) . '...';
                }

                $cont .= Html::tr(Html::td($quest, 'confclass', '', '40%') . Html::td($answ, 'confclass', '', '40%') . Html::td('<a href="' . LINK_ROOT . 'adminarea/?do=faq&amp;act=edit&amp;did=' . $qu['qid'] . '">' . __('Змінити') . '</a>', 'confclass', '', '10%') . Html::td('<a href="' . LINK_ROOT . 'adminarea/?do=faq&amp;act=del&amp;did=' . $qu['qid'] . '">' . __('Видалити') . '</a>', 'confclass', '', '10%'));
            }
            Aurus::getInstance()->addContent(Html::table($cont));
        }
    }
    public function runAjax(){}
}